﻿using MbUnit.Framework;
using BitTorrent.Bencode;
using BitTorrent.Bencode.Exceptions;
using BitTorrent.Bencode.Collections;

namespace UnitTests.Bencode
{
    /// <summary>
    /// Summary description for ListTests
    /// </summary>
    [TestFixture]
    public class ListTests
    {
        [Test]
        public void DecodeListOK() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("li42ei24ei442ee"));
            Assert.IsTrue(actual is List);
            var list = (List)actual;
            Assert.IsTrue(list.Count == 3);

            foreach (var integer in list) {
                Assert.IsTrue(integer is Integer);
            }

            Assert.IsTrue(((Integer)list[0]).Value == 42);
            Assert.IsTrue(((Integer)list[1]).Value == 24);
            Assert.IsTrue(((Integer)list[2]).Value == 442);
        }

        [Test]
        public void DecodeEmptyList() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("le"));
            Assert.IsTrue(actual is List);
            var list = (List)actual;
            Assert.IsTrue(list.Count == 0);
        }

        [Test]
        public void DecodeNestedEmptyList() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("llee"));
            Assert.IsTrue(actual is List);
            var list = (List)actual;
            Assert.IsTrue(list.Count == 1);
            Assert.IsTrue(((List)list[0]).Count == 0);
        }

        [Test]
        public void DecodeListWithManyEndings() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("leee"));
            Assert.IsTrue(actual is List);
            var list = (List)actual;
            Assert.IsTrue(list.Count == 1);
        }

        [Test]
        [ExpectedException(typeof(BencodeException))]
        public void DecodeListWithNoEnding() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("l1:a1:b1:c"));
        }
    }
}
